using System;
using System.Xml.Linq;
using TTEngine;
using TTEngine.Resource;
using Silk.NET.OpenGLES;

namespace TTEngine.Graphics
{
    public class ShaderObj : ResourceObj, IResource
    {

        public uint Handle { get; private set; }
        public ShaderType Type { get; private set; }
      
    
   
      
        public ShaderObj(ResourcePackage package, ShaderType type, string source)
            :base(package)
        {
            Type = type;
            Handle = TTEngine.Runtime.activeGL.CreateShader(type);
            if (Handle == 0)
            {
                throw new Exception($"AddShader error gl.CreateShader: {type}:{UniqueName}");
                return;
            }
            TTEngine.Runtime.activeGL.ShaderSource(Handle, source);
            TTEngine.Runtime.activeGL.CompileShader(Handle);
            var state = TTEngine.Runtime.activeGL.GetShader(Handle, ShaderParameterName.CompileStatus) > 0;
            if (!state)
            {
                string infoLog = TTEngine.Runtime.activeGL.GetShaderInfoLog(Handle);
                throw new Exception($"AddShader error gl.CompileShader: {infoLog}:{type}:{UniqueName}");
                return;
            }
        }
        protected override void OnDestory()
        {
            if (!HasDispose)
            {
                TTEngine.Runtime.activeGL.DeleteShader(Handle);
                HasDispose = true;
            }
        }



    }
}